//存放主要交互逻辑的js代码

var handler = {
	//封装相关ajax的url
	URL : {
        register : function () {
            return "/snake/auth/register";
        },
        login : function () {
            return "/snake/auth/login";
        },
        logout : function (access_token) {
            return "/snake/auth/logout?access_token="+access_token;
        },
        info : function (id, access_token) {
            if(id)
                return "/snake/user/"+id+"?access_token="+access_token;
            else
                return "/snake/user?access_token="+access_token;
        },
        updateUser : function (access_token) {
            return "/snake/user/update?access_token="+access_token;
        },
        password : function (access_token) {
            return "/snake/auth/password?access_token="+access_token;
        },
        board : function (access_token,type,mode) {
            if(type == 0) {//global
                return "/snake/score/gboard/"+mode+"?access_token="+access_token;
            } else {//friend
                return "/snake/score/fboard/"+mode+"?access_token="+access_token;
            }
        },
        ranking : function (access_token,type,mode) {
            if(type == 0) {//global
                return "/snake/score/granking/"+mode+"?access_token="+access_token;
            } else {//friend
                return "/snake/score/franking/"+mode+"?access_token="+access_token;
            }
        },
        addScore : function (access_token) {
            return "/snake/score/create?access_token="+access_token;
        },
        buySkin : function (equipmentId, access_token) {
            return "/snake/store/skin/"+equipmentId+"/buy?access_token="+access_token;
        },
        changeSkin : function (equipmentId, access_token) {
            return "/snake/store/skin/"+equipmentId+"/change?access_token="+access_token;
        },
        mySkin : function (access_token) {
            return "/snake/store/skin/my?access_token="+access_token;
        },
        share : function (access_token) {
            return "/snake/friend/share?access_token="+access_token;
        },
        friendList : function (page, access_token) {
            return "/snake/friend/list/"+page+"?access_token="+access_token;
        },
        friendRequest : function (friendId, access_token) {
            return "/snake/friend/"+friendId+"/request?access_token="+access_token;
        },
        friendAccept : function (friendId, access_token) {
            return "/snake/friend/"+friendId+"/accept?access_token="+access_token;
        },
        friendRemove : function (friendId, access_token) {
            return "/snake/friend/"+friendId+"/remove?access_token="+access_token;
        },
        messageList : function (page, access_token) {
            return "/snake/message/list/"+page+"?access_token="+access_token;
        },
        messageRead : function (id, access_token) {
            return "/snake/message/"+id+"/read?access_token="+access_token;
        },
        messageUnRead : function (id, access_token) {
            return "/snake/message/"+id+"/unread?access_token="+access_token;
        },
        messageDelete : function (id, access_token) {
            return "/snake/message/"+id+"/delete?access_token="+access_token;
        },

        test : function () {
            return "/snake/auth/test";
        },

        search : function (access_token) {
            return "/snake/user/search?access_token="+access_token;
        },




     //   todo:删除

        tempClearOrder: function() {
            return "/snake/store/temp_clear_order";
        }
	},

	register : {
        init : function (params) {
            $("#register").click(function () {
                $.ajax({
                    url:handler.URL.register(),
                    type:"POST",
                    dataType:"json",
                    data: {
                        "phone":$("#r-phone").val(),
                        "password":$("#r-password").val(),
                        "name":$("#r-name").val()
                    },
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                        $("#l-access-token").val(json.data.access_token);
                    },
                    complete: function () {
                    }
                })
            });
        }
    },
    login : {
        init : function (params) {
            $("#login").click(function () {
                $.ajax({
                    url:handler.URL.login(),
                    type:"POST",
                    dataType:"json",
                    data: {
                        "phone":$("#l-phone").val(),
                        "password":$("#l-password").val(),
                    },
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                        $(".access-token").val(json.data.access_token);
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    info : {
        init: function (params) {
            $("#info").click(function () {
                $.ajax({
                    url:handler.URL.info($("#i-user-id").val(), $("#i-access-token").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
            $("#my-info").click(function () {
                $.ajax({
                    url:handler.URL.info(undefined, $("#i-access-token").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    updateUser : {
        init: function (params) {
            $("#update-user").click(function () {
                $.ajax({
                    url:handler.URL.updateUser($("#update-user-access-token").val()),
                    type:"POST",
                    data:{
                        name : $("#update-user-name").val(),
                        sex : $("[name='update-user-sex']:checked").val()
                    },
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    logout : {
        init : function (params) {
            $("#logout").click(function () {
                $.ajax({
                    url:handler.URL.logout($("#l-access-token").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    password : {
        init: function (params) {
            $("#update-password").click(function () {
                $.ajax({
                    url:handler.URL.password($("#p-access-token").val()),
                    type:"POST",
                    data:{
                        "old_password":$("#old-password").val(),
                        "new_password":$("#new-password").val()
                    },
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    board : {
        init: function (params) {
            $("#board").click(function () {
                $.ajax({
                    url:handler.URL.board($("#b-access-token").val(), $("#b-type").val(), $("#b-mode").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    ranking : {
        init: function (params) {
            $("#ranking").click(function () {
                $.ajax({
                    url:handler.URL.ranking($("#r-access-token").val(), $("#r-type").val(), $("#r-mode").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    addScore : {
        init: function (params) {
            $("#add-score-begin-time, #add-score-end-time").datetimepicker({
                formatTime:'H:i:s',
                formatDate:'d.m.Y',
                defaultDate:'+03.01.1970', // it's my birthday
                defaultTime:'10:00:00',
                timepickerScrollbar:false
            });

            $("#add-score").click(function () {
                $.ajax({
                    url:handler.URL.addScore($("#add-score-access-token").val()),
                    type:"POST",
                    data:{
                        "score":$("#add-score-score").val(),
                        "mode":$("#add-score-mode").val(),
                        "attack":$("#add-score-attack").val(),
                        "length":$("#add-score-length").val(),
                        "begin_time" : $("#add-score-begin-time").val(),
                        "end_time" : $("#add-score-end-time").val()
                    },
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    store : {
        init: function (params) {
            $("#store").click(function () {
                $.ajax({
                    url:handler.URL.store($("#store-page").val(), $("#store-access-token").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    buySkin : {
        init: function (params) {
            $("#buy-skin").click(function () {
                $.ajax({
                    url:handler.URL.buySkin($("#buy-skin-id").val(), $("#buy-skin-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    changeSkin : {
        init: function (params) {
            $("#change-skin").click(function () {
                $.ajax({
                    url:handler.URL.changeSkin($("#change-skin-id").val(), $("#change-skin-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    mySkin : {
        init: function (params) {
            $("#my-skin").click(function () {
                $.ajax({
                    url:handler.URL.mySkin($("#my-skin-access-token").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    share : {
        init: function (params) {
            $("#share").click(function () {
                $.ajax({
                    url:handler.URL.share($("#share-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        $("#click-code").val(json.data);
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    click : {
        init : function () {
            $("#click").click(function () {
                handler.alert.show("已成生，请点击链接访问。");
                $("#click-link").attr("href", "snake/friend/share/" + $("#click-code").val());
            });
        }
    },

    friendList : {
        init: function (params) {
            $("#friend-list").click(function () {
                $.ajax({
                    url:handler.URL.friendList($("#friend-list-page").val(), $("#friend-list-access-token").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    friendRequest : {
        init: function (params) {
            $("#friend-request").click(function () {
                $.ajax({
                    url:handler.URL.friendRequest($("#friend-request-id").val(), $("#friend-request-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    friendAccept : {
        init: function (params) {
            $("#friend-accept").click(function () {
                $.ajax({
                    url:handler.URL.friendAccept($("#friend-accept-id").val(), $("#friend-accept-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },


    friendRemove : {
        init: function (params) {
            $("#friend-remove").click(function () {
                $.ajax({
                    url:handler.URL.friendRemove($("#friend-remove-id").val(), $("#friend-remove-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },
    messageList : {
        init: function (params) {
            $("#message-list").click(function () {
                $.ajax({
                    url:handler.URL.messageList($("#message-list-page").val(), $("#message-list-access-token").val()),
                    type:"GET",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    messageRead : {
        init: function (params) {
            $("#message-read").click(function () {
                $.ajax({
                    url:handler.URL.messageRead($("#message-read-id").val(), $("#message-read-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    messageUnRead : {
        init: function (params) {
            $("#message-unread").click(function () {
                $.ajax({
                    url:handler.URL.messageUnRead($("#message-unread-id").val(), $("#message-unread-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    messageDelete : {
        init: function (params) {
            $("#message-delete").click(function () {
                $.ajax({
                    url:handler.URL.messageDelete($("#message-delete-id").val(), $("#message-delete-access-token").val()),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },

    search : {
        init: function (params) {
            $("#search").click(function () {
                $.ajax({
                    url:handler.URL.search($("#message-delete-access-token").val()),
                    type:"POST",
                    data:{"name":"晓丽"},
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },


    tempClearOrder : {
        init: function (params) {
            $("#temp-clear-order").click(function () {
                $.ajax({
                    url:handler.URL.tempClearOrder(),
                    type:"POST",
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    },



    //共通方法
    alert : {
        init : function () {
            $("#alert-close").click(function () {
                $("#alert-message").html("");
                $("#alert").fadeOut();
            });
        },
        show : function (message) {
            $("#alert").fadeIn();
            $("#alert-message").html(message);
        }
    },

    test : {
        init : function () {
            $("#test").click(function () {
                $.ajax({
                    url:handler.URL.test(),
                    type:"POST",
                    data:{"user.phone":"1", "user.password":"1"},
                    datatype:"json",
                    success: function (json) {
                        handler.alert.show(JSON.stringify(json));
                    },
                    complete: function () {
                    }
                });
            });
        }
    }
}